package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.util.MyUtil;

import org.xutils.common.util.DensityUtil;

/**
 * Created by jiayen on 16/10/12.
 * 登录dialog
 */

public class CommDialog extends Dialog implements View.OnClickListener {

    protected View rootView;
    private TextView rightBtn;
    private TextView leftBtn;
    private TextView content;
    private EditText editText;
    private View line;
    private Object tag;

    private View.OnClickListener leftBtnListener;
    private View.OnClickListener rightBtnListener;
    private boolean leftClickDismiss;
    private boolean rightClickDismiss;
    private boolean showWithKeyword;

    public CommDialog(Context context) {
        super(context,R.style.custom_idalgo);
        rootView = getLayoutInflater().inflate(R.layout.comm_dialog_layout, null);
        init();
    }

    public void setRightListener(View.OnClickListener listener) {
        this.rightBtnListener = listener;
    }

    public void setLeftListener(View.OnClickListener listener){
        this.leftBtnListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(rootView);
        setCanceledOnTouchOutside(false);//点空白不消失
        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width= DensityUtil.getScreenWidth()/4*3;
        lp.height=  WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
    }

    private void init(){
        leftClickDismiss = true;
        rightClickDismiss = true;
        showWithKeyword = false;

        line = rootView.findViewById(R.id.middle_line);
        content = (TextView) rootView.findViewById(R.id.content);
        leftBtn = (TextView) rootView.findViewById(R.id.left_btn);
        rightBtn = (TextView) rootView.findViewById(R.id.right_btn);
        editText = (EditText) rootView.findViewById(R.id.edit_content);

        rightBtn.setOnClickListener(this);
        leftBtn.setOnClickListener(this);
    }

    public  void setEditHint(String hint){
        content.setVisibility(View.GONE);
        editText.setVisibility(View.VISIBLE);
        editText.setHint(hint);
    }

    public void hideCancleBtn(){
        leftBtn.setVisibility(View.GONE);
        line.setVisibility(View.GONE);
    }

    public void showCancleBtn(){
        leftBtn.setVisibility(View.VISIBLE);
        line.setVisibility(View.VISIBLE);
    }

    public void setTag(Object tag){
        this.tag = tag;
    }

    public Object getTag(){
        return this.tag;
    }

    public String getEditText(){
        return  editText.getText().toString();
    }

    public void setEditText(String text){
        if(text == null)
            return;
        editText.setText(text);
        editText.setSelection(text.length());
    }

    public void setContent(String str){
        content.setText(str);
    }

    public void setLeft(String str){
        leftBtn.setText(str);
    }

    public void setRight(String str){
        rightBtn.setText(str);
    }

    public void setLeftClickDismiss(boolean dismiss){
        this.leftClickDismiss = dismiss;
    }

    public void setRightClickDismiss(boolean dismiss){
        this.rightClickDismiss = dismiss;
    }

    public void setShowWithKeyword(boolean showWithKeyword){
        this.showWithKeyword = showWithKeyword;
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.left_btn){
            if(leftBtnListener != null)
                leftBtnListener.onClick(view);
            if(leftClickDismiss)
                dismiss();
        } else if(view.getId() == R.id.right_btn){
            if(rightBtnListener != null)
                rightBtnListener.onClick(view);
            if(rightClickDismiss)
                dismiss();
        }
    }

    @Override
    public void show() {
        try {
            super.show();
            if(showWithKeyword)
                editText.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        MyUtil.startKeyword(getContext(), editText);
                    }
                }, 300);
        }catch (Exception e){

        }
    }

    @Override
    public void dismiss() {
        if (editText.getVisibility() == View.VISIBLE)
            MyUtil.closeKeyword(getContext(), editText);
        super.dismiss();
    }
}
